package com.medata.utils;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.medata.dataObject.Globalsetting;
import com.medata.exeptions.YunExceptions;
import com.medata.service.GlobalsettingService;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import javax.xml.namespace.QName;

/**
 * @author ：zb
 * @date ：Created in 2019/5/29 0029 14:58
 * @description：
 */

public class SolrUtils {

    //删除索引核心的所有信息,并不会删除索引核心
    public static void deleteCore(GlobalsettingService globalsettingService,String core){
        Globalsetting globalsetting = globalsettingService.findByGsetname("IndexServerPath");
        String indexPath = globalsetting.getGsetvalue();
        String endPoint = indexPath + "/services/WebService?wsdl";
//        String endPoint="http://192.168.1.54:8080/TSK_SOLR/services/WebService?wsdl";
        String resultstr = "";
        try {
            RPCServiceClient client=new RPCServiceClient();
            Options options = client.getOptions();
            options.setTo(new EndpointReference(endPoint));
            options.setTimeOutInMilliSeconds(1000*60*5);
            options.setUserName("tsk");
            options.setPassword("123456");
            options.setAction("services");
            Object[] objects = client.invokeBlocking(new QName("deleteAllIndexWebService"), new Object[]{core}, new Class[]{String.class});
            resultstr = (String) objects[0];
            JsonParser parser = new JsonParser();
            JsonObject returns = parser.parse(resultstr).getAsJsonObject();
            String returnflag = returns.get("returnflag").getAsString();
            if (!returnflag.equals("0")) {
                throw new YunExceptions(0, "增加索引核心的时候失败");
            }
        } catch (Exception e) {
            throw new YunExceptions(0, "增加索引核心的时候出现bug");
        }
    }


    //增加索引核心-->新版本没问题
    public static void insertCore(GlobalsettingService globalsettingService,String core) {
        Globalsetting globalsetting = globalsettingService.findByGsetname("IndexServerPath");
        String indexPath = globalsetting.getGsetvalue();
        String endPoint = indexPath + "/services/WebService?wsdl";
//        String endPoint="http://192.168.1.54:8080/TSK_SOLR/services/WebService?wsdl";
        String resultstr = "";
        try {
            RPCServiceClient client=new RPCServiceClient();
            Options options = client.getOptions();
            options.setTo(new EndpointReference(endPoint));
            options.setTimeOutInMilliSeconds(1000*60*5);
            options.setUserName("tsk");
            options.setPassword("123456");
            options.setAction("services");
            Object[] objects = client.invokeBlocking(new QName("insertCoreWebService"), new Object[]{core}, new Class[]{String.class});
            resultstr = (String) objects[0];
            JsonParser parser = new JsonParser();
            JsonObject returns = parser.parse(resultstr).getAsJsonObject();
            String returnflag = returns.get("returnflag").getAsString();
            if (!returnflag.equals("0")) {
                throw new YunExceptions(0, "增加索引核心的时候失败");
            }
        } catch (Exception e) {
            throw new YunExceptions(0, "增加索引核心的时候出现bug");
        }
    }

    //删除单挑索引-->新版本没问题
    public static void deleteIndex(GlobalsettingService globalsettingService,String core,String id){
        Globalsetting globalsetting = globalsettingService.findByGsetname("IndexServerPath");
        String indexPath = globalsetting.getGsetvalue();
        String endPoint = indexPath + "/services/WebService?wsdl";
//        String endPoint="http://192.168.1.54:8080/TSK_SOLR/services/WebService?wsdl";
        String resultstr = "";
        try {
            RPCServiceClient client=new RPCServiceClient();
            Options options = client.getOptions();
            options.setTo(new EndpointReference(endPoint));
            options.setTimeOutInMilliSeconds(1000*60*5);
            options.setUserName("tsk");
            options.setPassword("123456");
            options.setAction("services");
            Object[] objects = client.invokeBlocking(new QName("deleteIndexWebService"), new Object[]{core,id}, new Class[]{String.class});
            resultstr = (String) objects[0];
            JsonParser parser = new JsonParser();
            JsonObject returns = parser.parse(resultstr).getAsJsonObject();
            String returnflag = returns.get("returnflag").getAsString();
            if (!returnflag.equals("0")) {
                throw new YunExceptions(0, "删除单挑索引的时候失败");
            }
        } catch (Exception e) {
            throw new YunExceptions(0, "删除单挑索引的时候出现bug");
        }
    }

    //添加索引-->新版本没问题
    public static void insertIndexs(GlobalsettingService globalsettingService,int dbid, String core, int sortIndexFlag, String id) {
        Globalsetting globalsetting = globalsettingService.findByGsetname("IndexServerPath");
        String indexPath = globalsetting.getGsetvalue();
        String endPoint = indexPath + "/services/WebService?wsdl";
//        String endPoint = "http://192.168.1.54:8080/TSK_SOLR/services/WebService?wsdl";
        String resultstr="";
        String flag="1";
        try {
            RPCServiceClient client=new RPCServiceClient();
            Options options = client.getOptions();
            options.setTo(new EndpointReference(endPoint));
            options.setTimeOutInMilliSeconds(1000*60*5);
            options.setUserName("tsk");
            options.setPassword("123456");
            options.setAction("services");
            Object[] objects = client.invokeBlocking(new QName("insertIndexWebService"), new Object[]{dbid,core,sortIndexFlag,id}, new Class[]{String.class});
            resultstr = (String) objects[0];
            JsonParser parser = new JsonParser();
            JsonObject returns = parser.parse(resultstr).getAsJsonObject();
            String returnflag = returns.get("returnflag").getAsString();
            if(returnflag.equals("0"))
                flag="2";
        }catch (Exception e){
            throw new YunExceptions(0,"添加"+core+"库的时候,添加索引异常");
        }
    }

    //更新单挑索引-->新版本没问题
    public static void updateIndex(GlobalsettingService globalsettingService,int dbid, String core, int sortIndexFlag, String id){
        Globalsetting globalsetting = globalsettingService.findByGsetname("IndexServerPath");
        String indexPath = globalsetting.getGsetvalue();
        String endPoint = indexPath + "/services/WebService?wsdl";
//        String endPoint = "http://192.168.1.54:8080/TSK_SOLR/services/WebService?wsdl";
        String resultstr="";
        String flag="1";
        try {
            RPCServiceClient client=new RPCServiceClient();
            Options options = client.getOptions();
            options.setTo(new EndpointReference(endPoint));
            options.setTimeOutInMilliSeconds(1000*60*5);
            options.setUserName("tsk");
            options.setPassword("123456");
            options.setAction("services");
            Object[] objects = client.invokeBlocking(new QName("updateIndexWebService"), new Object[]{dbid,core,sortIndexFlag,id}, new Class[]{String.class});
            resultstr = (String) objects[0];
            JsonParser parser = new JsonParser();
            JsonObject returns = parser.parse(resultstr).getAsJsonObject();
            String returnflag = returns.get("returnflag").getAsString();
            if(returnflag.equals("0"))
                flag="2";
        }catch (Exception e){
            e.printStackTrace();
            throw new YunExceptions(0,"更新"+core+"库的时候,添加索引异常");
        }
    }

    //批量更新索引-->新版本没问题
    public static void insertAllIndexs(GlobalsettingService globalsettingService,String core,Integer dbid){
        Globalsetting globalsetting = globalsettingService.findByGsetname("IndexServerPath");
        String indexPath = globalsetting.getGsetvalue();
        String endPoint = indexPath + "/services/WebService?wsdl";
//        String endPoint="http://192.168.1.54:8080/TSK_SOLR/services/WebService?wsdl";
        String resultstr = "";
        try {
            RPCServiceClient client=new RPCServiceClient();
            Options options = client.getOptions();
            options.setTo(new EndpointReference(endPoint));
            options.setTimeOutInMilliSeconds(1000*60*5);
            options.setUserName("tsk");
            options.setPassword("123456");
            options.setAction("services");
            Object[] objects = client.invokeBlocking(new QName("insertIndexsWebService"), new Object[]{dbid,core,0}, new Class[]{String.class});
            resultstr = (String) objects[0];
            JsonParser parser = new JsonParser();
            JsonObject returns = parser.parse(resultstr).getAsJsonObject();
            String returnflag = returns.get("returnflag").getAsString();
            if (!returnflag.equals("0")) {
                throw new YunExceptions(0, "增加索引核心的时候失败");
            }
        } catch (Exception e) {
            throw new YunExceptions(0, "增加索引核心的时候出现bug");
        }
    }








//    public static void main(String[] args) {
//        SolrUtils.updateIndex(null,3,"gszz",0,"43");
//    }

}
